package com.bianxianmao.duobao.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bianxianmao.R;
import com.bianxianmao.utils.Constants;
import com.bianxianmao.xxbei.Config;
import com.squareup.okhttp.Request;
import com.xiaoxiaopay.mp.XxBeiAPI;
import com.xiaoxiaopay.mp.XxBeiOrder;
import com.xiaoxiaopay.mp.XxBeiResult;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.math.BigDecimal;

//充值

public class RechargeActivity extends Activity implements View.OnClickListener {
    // private EditText orderId, orderName, orderPrice;

    private Button recharge;
    private ImageView ivrecharge;
    private RecyclerView recyclerView;
    private RecyclerView.ViewHolder holder;
    private RadioGroup rgmoney1;
    private RadioGroup rgmoney2;
    private RadioButton rbChild1;
    private RadioButton rbChild2;
    private EditText etsum;
    private RadioButton slectRdo;
    private String sum;
    private boolean isNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        initView();
        initListener();
    }

    private void initListener() {

    }

    private void initView() {

    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    private void merID() {
        PackageManager manager = getPackageManager();
        Bundle metaData = null;
        try {
            metaData = manager.getApplicationInfo("com.bianxianmao", PackageManager.GET_META_DATA).metaData;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (metaData != null) {
            String merID = String.valueOf(metaData.getInt("XXP_MER_ID"));
//            TextView appidView = (TextView) findViewById(R.id.text_merid);
//            appidView.setText("Manifest:XXP_MER_ID====>商户号:" + merID);
        }
    }

    public void ali() {
//        int childPosition = recyclerView.getChildPosition(recyclerView.getFocusedChild());
//        Log.i("AAA", "ali: "+childPosition);
        OkHttpUtils.get().url(Constants.RECHARGEORDERURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Log.i("ABC", "onError: ");
            }

            @Override
            public void onResponse(String response) {
                Log.i("TT", "onResponse: " + response);
            }
        });

        XxBeiOrder order = new XxBeiOrder();// 创建订单

        order.setOrderId("149934788");// 写入订单号

        order.setOrderName("玩具");// 写入商品名

        order.setPrice(new BigDecimal(200));// 写入总金额

        order.setNotifyurl("http://www.xiaoxiaopay.com");// 写入异步地址

        order.setPayType(10008);// 选择支付方式

        order.setPrivateKey(Config.privateKey);// 进行订单签名

        XxBeiAPI.pay(this, order, new XxBeiResult() {
            @Override
            public void payResult(int resultCode, String text) {
//                //Log.i("TAG", resultCode + "--------" + text);// 打印同步结果
                Toast.makeText(RechargeActivity.this, resultCode + "" + text, Toast.LENGTH_LONG).show();
                query(null);
            }
        });
    }

    public void query(View v) {
        XxBeiAPI.queryPay("149934788", Config.privateKey, new XxBeiResult() {
            @Override
            public void payResult(int resultCode, String text) {
                Log.i("TAG", "查询" + resultCode + "--------" + text);
                Toast.makeText(RechargeActivity.this, "resultCode:" + resultCode + "<-->text:" + text, Toast.LENGTH_LONG)
                        .show();
            }
        });
    }

//    public void reset(View v) {
//        orderId.setText(String.valueOf(System.currentTimeMillis()));
//    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
//            case R.id.iv_recharge:
//                startActivity(new Intent(this,WednesdayActivity.class));
//                break;
            case R.id.btn_recharge:
                isNum = sum.matches("[0-9]+");

//                if (isNum ==false){
//                    Toast.makeText(this,"请输入大于1的整数",Toast.LENGTH_SHORT).show();
//                    break;
//
//                }
                ali();

                break;

        }
    }

}
